﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Results_Comparer
{
    /// <summary>
    /// This class writes the difference view formatter
    /// </summary>
    public static class DifferenceViewFormatter
    {   
        /// <summary>
        /// Add additions to our display
        /// </summary>
        /// <param name="sW"></param>
        public static void AddAdditions(StreamWriter sW)
        {
            CreateDisplay("Additions", Difference.additions, sW);
        }

        /// <summary>
        /// Add changes to our display
        /// </summary>
        /// <param name="f"></param>
        public static void AddChanges(StreamWriter f)
        {
            CreateDisplay("Changes", Difference.changes, f);
        }

        
        /// <summary>
        /// Add deletions to our display
        /// </summary>
        /// <param name="f"></param>
        private static void AddDeletions(StreamWriter f)
        {
            CreateDisplay("Deletions", Difference.deletions, f);
        }

     
        /// <summary>
        /// Add an rdf:ID
        /// </summary>
        /// <param name="kvp"></param>
        /// <param name="sW"></param>
        private static void AddRdfId(KeyValuePair<RdfId, List<RdfAttribute>> kvp, StreamWriter sW)
        {
            sW.Write("\n<ul>");
            sW.Write("\n<li><A href=\"contents.html#" + kvp.Key.GetRdfId() + "\" target=\"contents\" >" + kvp.Key.getPathName() + " (" + kvp.Key.GetRdfId() + ")</A></li>");
            sW.Write("\n</ul>");
        }

        /// <summary>
        /// Write a table header
        /// </summary>
        /// <param name="sW"></param>
        private static void AddTableHeader(StreamWriter sW)
        {
            sW.Write("\n<TABLE WIDTH=\"100%\" BORDER=\"0\"><TR><TD WIDTH=\"100%\" ALIGN=\"LEFT\" VALIGN=\"TOP\">");
            sW.Write("\n<A href=\"#\" onClick=\"expandDifferences('tree1'); return false;\">Expand All</A>&nbsp;&nbsp;&nbsp;");
            sW.Write("\n<A href=\"#\" onClick=\"collapseDifferences('tree1'); return false;\">Collapse All</A>&nbsp;&nbsp;&nbsp;");
            sW.Write("\n<ul class=\"mktree\" id=\"tree1\">");
        }

        /// <summary>
        /// Create a display
        /// </summary>
        /// <param name="str"></param>
        /// <param name="dict"></param>
        /// <param name="sW"></param>
        private static void CreateDisplay(string str, Dictionary<RdfId, List<RdfAttribute>> dict, StreamWriter sW)
        {
            string str2 = string.Empty;
            sW.Write(string.Concat(new object[] { "<li>", str, " (", dict.Count, ")\n" }));
            sW.Write("<ul>\n");
            foreach (KeyValuePair<RdfId, List<RdfAttribute>> pair in dict.OrderBy<KeyValuePair<RdfId, List<RdfAttribute>>, RdfId>(kvp => kvp.Key, new TypeComparer()))
            {
                string rdfType = pair.Key.rdfType;
                if (rdfType.Equals(str2))
                    AddRdfId(pair, sW);                
                else
                {
                    sW.Write(string.Concat(new object[] { "\n<li>", pair.Key.rdfType, " (", Difference.GetCounts(dict, pair.Key.rdfType), ")" }));
                    AddRdfId(pair, sW);
                    sW.Write("\n</li>");
                }
                str2 = rdfType;
            }
            sW.Write("\n</ul>");
            sW.Write("</li>");
        }
        
        /// <summary>
        /// Write the navigation JavaScript
        /// </summary>
        /// <param name="sW"></param>
        /// <param name="InScript"></param>
        public static void WriteNavJS(StreamWriter sW, string InScript)
        {
            sW.Write("<html>\n");
            sW.Write("<head>\n");
            sW.Write("<h4 align=\"center\">Difference View</h4>\n");
            sW.Write("<SCRIPT LANGUAGE=\"JavaScript\">");
            sW.WriteLine(InScript);
            sW.Write("</SCRIPT>");
            sW.Write("<style>ul.mktree  li { list-style: none; } ul.mktree, ul.mktree ul , ul.mktree li{ margin-left:10px; padding:0px; } ul.mktree  li           .bullet { padding-left: 15px; } ul.mktree  li.liOpen    .bullet { cursor: pointer; background: url(minus.gif)  center left no-repeat; } ul.mktree  li.liClosed  .bullet { cursor: pointer; background: url(plus.gif)   center left no-repeat; } ul.mktree  li.liBullet  .bullet { cursor: default; background: url(bullet.gif) center left no-repeat; } ul.mktree  li.liOpen    ul { display: block; } ul.mktree  li.liClosed  ul { display: none; } </style>");
            sW.Write("</head>\n");
            sW.Write("<body  BGCOLOR=#FFFFFF LINK=\"#00615F\" VLINK=\"#00615F\" ALINK=\"#00615F>\n");
            AddTableHeader(sW);
            AddAdditions(sW);
            AddChanges(sW);
            AddDeletions(sW);
            sW.Write("</TD><TD VALIGN=\"TOP\"></TD></TR></TABLE> \n");
            sW.Write("</body>\n");
            sW.Write("</html>\n");
        }
    }
}
